package com.warm.orm.config;

import cn.hutool.core.util.ObjUtil;
import com.easy.query.core.configuration.QueryConfiguration;
import com.easy.query.solon.annotation.Db;
import com.warm.orm.extension.DeleteFlagStrategy;
import com.warm.orm.interceptor.EqEntityInterceptor;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.dynamicds.DynamicDataSource;

import javax.sql.DataSource;

/**
 * MybatisFlex配置
 *
 * @author warm
 */
@Configuration
public class EasyQueryConfig {

    /**
     * 动态数据源（直接注入）
     *
     * @param dataSource 数据源
     * @return
     */
    @Bean(typed = true)
    public DataSource db1(@Inject("${dataSource.db1}") DynamicDataSource dataSource) {
        return dataSource;
    }

    @Bean
    public void eqQueryConfiguration(@Db QueryConfiguration configuration) {
        // 配置逻辑删除策略
        if (ObjUtil.isNull(configuration.getLogicDeleteStrategy(DeleteFlagStrategy.STRATEGY_NAME))) {
            configuration.applyLogicDeleteStrategy(new DeleteFlagStrategy());
        }
        configuration.applyInterceptor(new EqEntityInterceptor());
    }

}
